/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package motiveai.tests;

import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import java.util.ArrayList;
import java.util.Iterator;
import motiveai.components.SensorEntity;
import motiveai.factors.Environment;

/**
 *
 * @author oxplay
 */
public class Test2Sensor extends SensorEntity<Test2Agent> {

    private ArrayList<Node> gameCharacters = new ArrayList<Node>();

    public Test2Sensor(ArrayList<Node> gameCharacters){
        this.gameCharacters = gameCharacters;
    }
    
    @Override
    protected void examEnvironment(Test2Agent agent, Environment environment) {
        long actualTime = System.currentTimeMillis();
        environment.setBooleanFactor("isAttacking", false);
        if (actualTime - agent.lastBoringAdded > 6000) {
            agent.lastBoringAdded = actualTime;
            environment.setBooleanFactor("isBored", true);
        }
        environment.setBooleanFactor("isEnemyClose", false);
        agent.closeEnemy = null;
        for (Iterator<Node> it = gameCharacters.iterator(); it.hasNext();) {
            Node node = it.next();
            if (!node.equals(agent.getSpatial()) && node.<Integer>getUserData("health") > 0) {
                float distance = agent.getSpatial().getLocalTranslation().distance(node.getLocalTranslation());
                System.out.println("distance: " + distance);
                if (distance < 15f) {
                    System.out.println("enemy set to node: " + node);
                    agent.closeEnemy = node;
                    System.out.println("enemy node: " + agent.closeEnemy);
                    environment.setBooleanFactor("isEnemyClose", true);
                }
            }
        }
        float distanceFromChest = agent.getSpatial().getLocalTranslation().distance(new Vector3f(0,agent.getSpatial().getLocalTranslation().y,0));
        System.out.println("distance from chest: " + distanceFromChest);
        if(distanceFromChest > 10f){
            environment.setBooleanFactor("isDefendingChest", false);
        }
    }
}
